<?php

namespace App\Actor;

use EasySwoole\Component\TableManager;
use Swoole\Table;

class ChatManager
{
    /**
     * 初始化一个table用来存储连接信息
     */
    public static function tableInit()
    {
        TableManager::getInstance()->add('chat_list', [
            'actorId' => [
                'type' => Table::TYPE_STRING,
                'size' => 25
            ],
            'uid' => [
                'type' => Table::TYPE_INT,
                'size' => 4
            ],
            'fd' => [
                'type' => Table::TYPE_INT,
                'size' => 4
            ],
            'params' => [
                'type' => Table::TYPE_STRING,
                'size' => 200
            ]
        ], 1024);
    }

    public static function chatInfo(int $uid): ?ChatBean
    {
        $ret = self::getTable()->get($uid);
        if (is_array($ret)) {
            return new ChatBean($ret);
        }
        return null;
    }

    public static function deleteChat(int $uid)
    {
        self::getTable()->del($uid);
    }

    public static function updateChatInfo(int $uid, array $data)
    {
        self::getTable()->set($uid, $data);
    }

    public static function addChat(ChatBean $bean)
    {
        self::getTable()->set($bean->getUid(), $bean->toArray());
    }

    public static function getTable(): ?Table
    {
        return TableManager::getInstance()->get('chat_list');
    }
}